﻿using System;
using System.Xml;

namespace GRIP.Gestures
{
    public class ZoomGesture : ComplexGesture<ZoomGesture>
    {
        public DragA LeftDrag
        {
            get;
            private set;
        }
        public DragA RightDrag
        {
            get;
            private set;
        }
        public double Magnitude;
        const double MOVEMENT_THRESHOLD = 0.001;

        public ZoomGesture(DateTime Time, long ID, string Source, DragA left, DragA right)
            : base(Time, ID, Source, left, right)
        {
            LeftDrag = left;
            RightDrag = right;
            //Update();
        }

        public override bool Update()
        {
            base.Update();
            double distance = Vector3.Distance2D(LeftDrag.StartPosition, RightDrag.StartPosition);
            double difference1 = Vector3.Distance2D(LeftDrag.CurrentPosition, RightDrag.StartPosition) - distance;
            double difference2 = Vector3.Distance2D(RightDrag.CurrentPosition, LeftDrag.StartPosition) - distance;
            double magnitude = (float)(difference1 + difference2) * 0.5f;

            if (Math.Abs(Magnitude - magnitude) > MOVEMENT_THRESHOLD)
            {
                Magnitude = magnitude;
            }
            return true;
        }

        public override bool ChildRemoved(BasicGesture g)
        {
            Destroy(DateTime.Now);
            return true;
        }

        public override XmlElement ToXML()
        {
            XmlDocument doc = new XmlDocument();
            XmlElement element = doc.CreateElement("ZoomGesture");
            element.SetAttribute("ID", ID.ToString());
            element.SetAttribute("Magnitude", Magnitude.ToString());
            element.SetAttribute("StartTime", StartTime.ToString());
            element.SetAttribute("Source", Source);
            return element;
        }
    }
}
